package com.angel.utils;

import com.angel.shiro.SystemAdminAuthorizingRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;

public class AdminUserUtils {
//    private static SysUserService sysUserService = SpringUtil.getBean(SysUserService.class);

    /**
     * 获取当前登录者对象
     */
    public static SystemAdminAuthorizingRealm.Principal getPrincipal() {
        try {
            Subject subject = SecurityUtils.getSubject();
            SystemAdminAuthorizingRealm.Principal principal = (SystemAdminAuthorizingRealm.Principal) subject.getPrincipal();
            if (principal != null) {
                return principal;
            }
        } catch (UnavailableSecurityManagerException e) {

        } catch (InvalidSessionException e) {

        }
        return null;
    }


    /**
     * 根据ID获取用户
     * @param id
     * @return 取不到返回null
     */
//    public static SysUser get(Integer id){
//        SysUser sysUser=new SysUser();
//        sysUser.setId(id);
//        return sysUserService.findOne(sysUser);
//    }
//    /**
//     * 获取当前用户
//     * @return 取不到返回 new User()
//     */
//    public static SysUser getUser(){
//        SystemAdminAuthorizingRealm.Principal principal = getPrincipal();
//        if (principal!=null){
//            SysUser user = get(principal.getId());
//            if (user != null){
//                return user;
//            }
//            return new SysUser();
//        }
//        // 如果没有登录，则返回实例化空的User对象。
//        return new SysUser();
//    }


}
